package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysDict;
import cn.wgx.modules.sys.services.SysDictService;
import cn.wgx.modules.sys.util.DictUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Api(tags = "6. 团队管理", value = "value",
        protocols = "protocols", produces = "produces", consumes = "consumes")
//@RestController
//@RequestMapping(value="${allPathPre}team")
public class TeamController extends BaseController {

    @Resource
    SysDictService sysDictService;

    @ApiOperation(value = "列表",notes = "获取团队列表. 返回: k为主键,v为团队名称. 用户信息提交k.   title 选择根据哪个字段名排序")
    @RequestMapping(path = "list", method = RequestMethod.GET)
    @ApiPermission(false)
    public Map<String, Object> getList(@ApiParam("团队名称,模糊查询") @RequestParam(required = false) String v,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "20") Integer pageSize,
                                       @ApiParam(value = "选择排序时必填根据哪个排序") @RequestParam(defaultValue = "sort,createDate") String title,
                                       @RequestParam(defaultValue = "desc") String order) {
        SysDict sysDict = new SysDict();
        sysDict.setType(DictUtil.TYPE_TEAM);
        if(StringUtils.isNotBlank(v)){
            sysDict.setV(v);
        }
        return returnData(PageHelper.startPage(page, Math.min(Math.abs(pageSize), maxPageSize)), sysDictService.getList(sysDict,title,order));
    }


    @ApiOperation(value = "新增", notes = "只需要参数: v ,代表团队名称. 其他参数可选")
    @RequestMapping(path = "insert", method = RequestMethod.POST)
    @ApiPermission
    public Map<String, Object> insertOne(@RequestBody SysDict sysDict) {
        if(sysDict.getV() == null){
            return returnErrorMap("参数错误");
        } else if(!sysDictService.checkV(sysDict.getV())){
            return returnErrorMap("团队名重复，请重新输入");
        }
        sysDict.setType(DictUtil.TYPE_TEAM);
        sysDict.setK(UUID.randomUUID().toString().replaceAll("-", ""));
        return returnData(sysDictService.insertOne(sysDict));
    }


    @ApiOperation(value = "更新", notes = "必须参数: dict_id, v.  其他可选.")
    @RequestMapping(path = "update", method = RequestMethod.PATCH)
    @ApiPermission
    public Map<String, Object> updateOne(@RequestBody SysDict sysDict) {
        if(sysDict.getDict_id() == null || StringUtils.isBlank(sysDict.getV())){
            return returnErrorMap("参数错误.");
        }
        SysDict dict = new SysDict();
        dict.setV(sysDict.getV());
        List<SysDict> list = sysDictService.getList(dict,"-1",null);    //   title = -1 时根据v精确查询部门
        for (SysDict dc:list) {
            if(list.size() >= 1 && !dc.getDict_id().equals(sysDict.getDict_id()) ){
                return returnErrorMap("团队名重复，请重新输入");
            }
        }
        sysDict.setType(DictUtil.TYPE_TEAM);
        return returnData(sysDictService.updateOne(sysDict));

    }

    @ApiOperation("删除")
    @RequestMapping(path = "del/{id}", method = RequestMethod.DELETE)
    @ApiPermission
    public Map<String, Object> deleteOne(@PathVariable("id") Integer id) {
        return returnData(sysDictService.deleteOne(id));
    }

    @ApiOperation("刷新缓存")
    @RequestMapping(path = {"flush"}, method = RequestMethod.DELETE)
    @ApiPermission(false)
    public Map<String, Object> clearCache(@RequestParam(value = "type", required = false) String type) {
        return returnData(sysDictService.clearCache(type));
    }

}
